package com.dean.sql.service.impl;

import com.dean.sql.service.DeanSql;

/**
 * @ClassName DeanMysql
 * @Description
 * @Date 2022/1/1 13:57
 * @Created by dean
 */
public class DeanMssql implements DeanSql {
    @Override
    public String getTableInfoSql() {
        return  "select tab.name TABLE_NAME,cast(des.value as varchar(512)) TABLE_COMMENT from sys.tables tab left join sys.extended_properties des on tab.object_id = des.major_id and des.minor_id=0 where tab.name = ?";
    }

    @Override
    public String getFieldInfoSql() {
        return "SELECT c.name COLUMN_NAME,cast(ep.value as varchar(512)) COLUMN_COMMENT,tp.name DATA_TYPE,\n" +
                "case when c.precision > 0 then c.precision  else  c.max_length end  DATA_LENGTH,\n" +
                "case when c.scale = 0 then null else c.scale end DATA_SCALE\n" +
                ",'N' PK_FLAG FROM sys.tables t\n" +
                "INNER JOIN sys.columns\n" +
                "AS c ON t.object_id = c.object_id\n" +
                "LEFT JOIN sys.extended_properties ep\n" +
                "ON ep.major_id = c.object_id AND ep.minor_id = c.column_id \n" +
                "LEFT JOIN sys.types tp\n" +
                "ON tp.user_type_id = c.user_type_id\n" +
                "where t.name = ? order by c.column_id";
    }

    @Override
    public String getPageSql(String sql) {
        String exeSql = "select top ? * from ("+sql+") t";
        return exeSql;
    }

    @Override
    public String covertTableName(String tableName) {
        return tableName.toLowerCase();
    }
}
